package com.tmrnavi.sensormeasureservice;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    public ToggleButton tb_service_switch;
    public SwitchCompat sb_data;
    public TextView[] tv_sensors_list = new TextView[10];
    public static MsgHandler msgHandler;
    // 消息类型
    public static final int DISPMSG_SENSOR = 101;               // 显示传感器数据
    public static final int DISPMSG_AHRS = 102;                 // 显示航姿数据
    public static final int DISPMSG_SINS = 103;                 // 显示导航数据
    public static final int DISPMSG_XKPK = 104;                 // 2.0.1后不再显示KF数据

    public boolean isSave = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_sensors_list[0] = findViewById(R.id.tv_wx);
        tv_sensors_list[1] = findViewById(R.id.tv_wy);
        tv_sensors_list[2] = findViewById(R.id.tv_wz);
        tv_sensors_list[3] = findViewById(R.id.tv_ax);
        tv_sensors_list[4] = findViewById(R.id.tv_ay);
        tv_sensors_list[5] = findViewById(R.id.tv_az);
        tv_sensors_list[6] = findViewById(R.id.tv_mx);
        tv_sensors_list[7] = findViewById(R.id.tv_my);
        tv_sensors_list[8] = findViewById(R.id.tv_mz);
        tv_sensors_list[9] = findViewById(R.id.tv_bar);
        tb_service_switch = findViewById(R.id.tb_service);
        sb_data = findViewById(R.id.sb_savedata);
        tb_service_switch.setOnClickListener(this);
        sb_data.setOnCheckedChangeListener(this);

        msgHandler = new MsgHandler();
    }

    // Method to start the service
    public void startService() {
        Intent serviceIntent = new Intent(getBaseContext(), SensorService.class);
        serviceIntent.putExtra("savedata", isSave);
        startService(serviceIntent);
    }

    // Method to stop the service
    public void stopService() {
        stopService(new Intent(getBaseContext(), SensorService.class));
    }

    /************************************************
     *   更新传感器数据显示
     ************************************************/
    private void SensorDataDisp(float[] data)
    {
        for (int i=0; i<10; i++){
            tv_sensors_list[i].setText(String.format("%8.3f", data[i]));
        }
    }

    @SuppressLint("SetTextI18n")
    protected void dataUpdate(double[] sensordata){
        for (int k=0; k<10; k++) {
            tv_sensors_list[k].setText(Double.toString(sensordata[k]));
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tb_service:
                if (tb_service_switch.isChecked()) {
                    sb_data.setEnabled(false);
                    startService();
                }
                else {
                    sb_data.setEnabled(true);
                    stopService();
                }
                break;
            default:
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        isSave = b;
    }

    // 消息处理类
    private class MsgHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.arg1){
                case DISPMSG_SENSOR:
                    float[] sensor_data = msg.getData().getFloatArray("data");
                    SensorDataDisp(sensor_data);
                    break;
//                case DISPMSG_AHRS:
//                    AHRSDataDisp(att);
//                    break;
//                case DISPMSG_SINS:
//                    AHRSDataDisp(avppx);
//                    break;
            }
        }
    }
}